package com.shawn.server.service;

import com.alibaba.fastjson.JSON;
import com.shawn.commons.utils.RC;
import com.shawn.server.annotation.LogConfig;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
//@Aspect
public class MqMessageService {
    @Resource
    private AmqpTemplate amqpTemplate;

    /**
     * 发送日志消息
     *
     * @param userId
     * @param type 操作类型,
     *            0101为登录，
     *            0102为注册，
     *            0201为发动态，
     *            0202为浏览动态，
     *            0203为动态点赞，
     *            0204为动态喜欢，
     *            0205为评论，
     *            0206为动态取消点赞，
     *            0207为动态取消喜欢，
     *            0301为发小视频，
     *            0302为小视频点赞，
     *            0303为小视频取消点赞，
     *            0304为小视频评论
     * @param key      用户相关user , 动态相关movement , 小视频相关 video
     * @param busId   业务id  动态id或者视频id
     */
        public void sendLogMessage(Long userId,String type,String key,String busId){
        try {
            Map map = new HashMap<>();
            map.put("userId",userId.toString());
            map.put("type",type);
            map.put("busId",busId); //动态/视频的id
            map.put("logTime",new SimpleDateFormat(RC.GENERAL_DATE_FORMAT).format(new Date()));
            String message = JSON.toJSONString(map);
            amqpTemplate.convertAndSend("touch.log.exchange","log."+key,message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }


    /**
     * 消息发送访问者数据
     *
     * @param userId    用户id
     * @param visitorId 游客id
     * @param from      来源
     */
    public void sendVisitorDataMessage(Long userId, Long visitorId,String from) {
        try {
            Map<String,Object> messageMap = new HashMap<>();
            messageMap.put("userId", userId);
            messageMap.put("from",from);
            messageMap.put("visitorId", visitorId);
            String message = JSON.toJSONString(messageMap);
            amqpTemplate.convertAndSend(RC.MQ_VISITOR_EXCHANGE,RC.MQ_VISITOR_KEY,message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        log.info("访客数据已发送");
    }




    /**
     * 发送动态审核消息
     *
     * @param movementId 运动id
     */
    public void sendAudiMessage(String movementId) {
        try {
            amqpTemplate.convertAndSend("touch.audit.exchange",  "audit.movement",movementId);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }
}
